package com.album.controller;


import com.album.auto.JSONAuthentication;
import com.album.entity.AlAl;
import com.album.entity.AlImg;
import com.album.mapper.AlAlMapper;
import com.album.mapper.AlImgMapper;
import com.album.response.Result;
import com.album.utils.UpPhotoNameUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author album
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/al-img")
public class AlImgController extends JSONAuthentication {
    @Autowired
    AlImgMapper alImgMapper;
    @Autowired
    AlAlMapper alAlMapper;
    @ApiOperation(value = "添加相册照片")
    @PostMapping("/add")
    public void addPhoto(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("alId") int  alId) throws Exception {
        AlImg alImg=new AlImg();
        alImg.setAlId(alId);
            byte[] bytes = file.getBytes();
            String imageFileName = file.getOriginalFilename();
            String fileName = UpPhotoNameUtils.getPhotoName("img", imageFileName);
            Path path = Paths.get("D:\\自己的项目\\订餐系统\\order\\src\\assets\\picture\\" + fileName);
            //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
            Files.write(path, bytes);//写入文件
            String avatar_url = fileName;
            alImg.setImg(avatar_url);
            response.reset();
            if(alImgMapper.insert(alImg)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
    }
    @ApiOperation(value = "将相册照片移入回收站")
    @PostMapping("/move")
    public void delPhoto(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        AlImg alImg = alImgMapper.selectById(id);
        alImg.setStatus("回收站");
        if (alImgMapper.updateById(alImg) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "将回收站照片删除")
    @PostMapping("/del")
    public void delUseraaa(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        if ( alImgMapper.deleteById(id)> 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过相册id查找未进入回收箱的照片")
    @PostMapping("/findImgNoBox")
    public void findImgNoBox(HttpServletRequest request, HttpServletResponse response, @RequestParam("alID") int  alID, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status","展示");
        wrapper.eq("al_id",alID);
        Page<AlImg> page = new Page<>(size, current);
        Page<AlImg> pages = alImgMapper.selectPage(page, wrapper);
        List<AlImg> list = pages.getRecords();
        int total = alImgMapper.selectCount(wrapper);
        for (AlImg a:list){
            a.setAlAl(alAlMapper.selectById(a.getAlId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("用户相册没有照片");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "通过相册id查找进入回收箱的照片")
    @PostMapping("/findImgBox")
    public void findImgBox(HttpServletRequest request, HttpServletResponse response, @RequestParam("alID") int  alID, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status","回收站");
        wrapper.eq("al_id",alID);
        Page<AlImg> page = new Page<>(size, current);
        Page<AlImg> pages = alImgMapper.selectPage(page, wrapper);
        List<AlImg> list = pages.getRecords();
        for (AlImg a:list){
            a.setAlAl(alAlMapper.selectById(a.getAlId()));
        }
        int total = alImgMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("用户相册没有照片");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "=查找全部进入回收箱的照片")
    @PostMapping("/findImgBoxAll")
    public void findImgBoxAll(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","回收站");
        Page<AlImg> page = new Page<>(size, current);
        Page<AlImg> pages = alImgMapper.selectPage(page, wrapper);
        List<AlImg> list = pages.getRecords();
        for (AlImg a:list){
            a.setAlAl(alAlMapper.selectById(a.getAlId()));
        }
        int total = alImgMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有照片");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "将相册恢复")
    @PostMapping("/start")
    public void start(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        AlImg alImg = alImgMapper.selectById(id);
        alImg.setStatus("展示");
        if (alImgMapper.updateById(alImg) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

